package com.exwave.socialgame.model_wolfgame.service;

import com.exwave.socialgame.model_wolfgame.dao.RoomsDao;
import com.exwave.socialgame.model_wolfgame.Rooms;
import com.exwave.socialgame.myutils.ReturnUtils;

import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;

/**
 * (Rooms)表服务实现类
 *
 * @author makejava
 * @since 2021-11-17 10:39:56
 */
@Service("roomsService")
public class RoomsServiceImpl implements RoomsService {
    @Resource
    private RoomsDao roomsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param rid 主键
     * @return 实例对象
     */
    @Override
    public Rooms queryById(Integer rid) {
        Rooms rooms = this.roomsDao.queryById(rid);
        ReturnUtils.isObjNull(rooms, "该房间不存在");
        return rooms;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Rooms> queryAllByLimit(int offset, int limit) {
        ReturnUtils.isTrue(offset < 0, "起始位置error");
        ReturnUtils.isTrue(limit <= 0, "查询条数error");
        List<Rooms> list = this.roomsDao.queryAllByLimit(offset, limit);
        ReturnUtils.isObjNull(list, "查询失败");
        return list;
    }

    /**
     * 新增数据
     *
     * @param rooms 实例对象
     * @return 实例对象
     */
    @Override
    public Rooms insert(Rooms rooms) {
        this.roomsDao.insert(rooms);
        return rooms;
    }

    /**
     * 修改数据
     *
     * @param rooms 实例对象
     * @return 实例对象
     */
    @Override
    public Rooms update(Rooms rooms) {
        this.roomsDao.update(rooms);
        return this.queryById(rooms.getRid());
    }

    /**
     * 通过主键删除数据
     *
     * @param rid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer rid) {
        Rooms rooms = this.roomsDao.queryById(rid);
        ReturnUtils.isObjNull(rooms,"房间不存在");
        return this.roomsDao.deleteById(rid) > 0;
    }
}